package com.beta.restapi.controller;

import com.beta.restapi.internal.Consts;
import com.beta.service.ICRegionLevelsService;
import com.beta.common.web.ContentType;
import com.beta.dto.region.CRegionDto;
import com.beta.vo.region.RegionVO;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by vector on 2017/2/22.
 */
@RestController
@RequestMapping(Consts.API_PATH_PREFIX)
public class RegionsController extends BaseController {

    @Autowired
    private ICRegionLevelsService cRegionLevelsService;

    @Autowired
    private MapperFacade beanMapper;

    @ApiOperation(value = "c_region_levels", notes = "省市区街道列表")
    @RequestMapping(value = "c_region_levels", produces = {ContentType.JSON})
    public Object getRegions(@RequestHeader(Consts.HEADER_AUTH_KEY) String token) {
        List<CRegionDto> topThreeLevelRegions = cRegionLevelsService.getTopThreeLevelRegions();
        return ok(beanMapper.mapAsList(topThreeLevelRegions, RegionVO.class));
    }

    @ApiOperation(value = "c_region_levels/{regionCode}", notes = "省市区街道列表")
    @RequestMapping(value = "c_region_levels/{regionCode}", produces = {ContentType.JSON})
    public Object getSubRegions(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                @PathVariable("regionCode") Integer regionCode) {
        List<CRegionDto> subRegions = cRegionLevelsService.getRegionsByParentId(regionCode);
        return ok(beanMapper.mapAsList(subRegions, RegionVO.class));
    }

}
